package com.lineage.data.item_etcitem.reel;

import com.lineage.config.Configtype;
import com.lineage.data.cmd.EnchantWeapon;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1ItemUpdata;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Random;

public class ScrollEnchantWeapon_a extends ItemExecutor {
  final Random random = new Random();
  
  public static ItemExecutor get() {
    return new ScrollEnchantWeapon_a();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
    if (tgItem == null)
      return; 
    if (tgItem.isEquipped()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("(预防误点机制启动)装备中无法强化"));
      return;
    } 
    if (tgItem.isSeal()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(tgItem.getLogName()) + "处于绑定状态！"));
      return;
    } 
    int safe_enchant = tgItem.getItem().get_safeenchant();
    boolean isErr = false;
    int use_type = tgItem.getItem().getUseType();
    switch (use_type) {
      case 1:
        if (safe_enchant < 0)
          isErr = true; 
        break;
      default:
        isErr = true;
        break;
    } 
    int weaponId = tgItem.getItem().getItemId();
    if (tgItem.getEnchantLevel() == Configtype.weaponlv) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("您的武器已达上限值。"));
      return;
    } 
    isErr = ((weaponId < 301 || weaponId > 315) && (weaponId < 1040 || weaponId > 1043));
    if (weaponId >= 100213 && weaponId <= 100217)
      isErr = true; 
    if (isErr) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("没发生什么事情。"));
      return;
    } 
    int enchant_level = tgItem.getEnchantLevel();
    EnchantWeapon enchantWeapon = new EnchantWeapon();
    int randomELevel = enchantWeapon.randomELevel(tgItem, item.getBless());
    boolean isEnchant = true;
    if (enchant_level < -6) {
      isEnchant = false;
    } else if (enchant_level < safe_enchant) {
      isEnchant = true;
    } else {
      int enchant_level_tmp, enchant_chance_wepon;
      Random random = new Random();
      int rnd2 = random.nextInt(100) + 1;
      if (safe_enchant == 0) {
        enchant_level_tmp = enchant_level + 6;
      } else {
        enchant_level_tmp = enchant_level;
      } 
      if (enchant_level >= 9) {
        enchant_chance_wepon = (int)L1ItemUpdata.enchant_wepon_up9(enchant_level_tmp);
      } else {
        enchant_chance_wepon = (int)L1ItemUpdata.enchant_wepon_dn9(enchant_level_tmp);
      } 
      if (rnd2 < enchant_chance_wepon) {
        isEnchant = true;
      } else if (enchant_level >= 9 && rnd2 < enchant_chance_wepon * 2) {
        randomELevel = 0;
      } else {
        isEnchant = false;
      } 
    } 
    if (randomELevel <= 0 && enchant_level > -6)
      isEnchant = true; 
    if (isEnchant) {
      enchantWeapon.successEnchant(pc, tgItem, randomELevel);
      tgItem.setproctect(false);
      tgItem.setproctect1(false);
      tgItem.setproctect2(false);
      tgItem.setproctect3(false);
      tgItem.setproctect4(false);
      tgItem.setproctect5(false);
      pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
      pc.getInventory().saveItem(tgItem, 4);
      pc.setcheck_lv(false);
      pc.getInventory().removeItem(item, 1L);
    } else {
      if (tgItem.getproctect()) {
        tgItem.setproctect(false);
        pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
        pc.getInventory().updateItem(tgItem, 4);
        pc.getInventory().saveItem(tgItem, 4);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("受到高级装备保护卷轴的影响,失败后物品无变化。"));
        RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), item.getId(), 
            "受到高级卷的保护", pc.getIp());
      } else if (tgItem.getproctect1()) {
        tgItem.setproctect1(false);
        tgItem.setEnchantLevel(tgItem.getEnchantLevel() - 1);
        pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
        pc.getInventory().updateItem(tgItem, 4);
        pc.getInventory().saveItem(tgItem, 4);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("受到中级装备保护卷轴的影响,失败后物品倒扣1。"));
        RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), item.getId(), 
            "受到中级卷的保护", pc.getIp());
      } else if (tgItem.getproctect2()) {
        tgItem.setproctect2(false);
        tgItem.setEnchantLevel(0);
        pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
        pc.getInventory().updateItem(tgItem, 4);
        pc.getInventory().saveItem(tgItem, 4);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("受到初级装备保护卷轴的影响,失败后物品归0。"));
        RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), item.getId(), 
            "受到低级卷的保护", pc.getIp());
      } else if (tgItem.getproctect3()) {
        if (this.random.nextInt(100) + 1 <= pc.getproctctran()) {
          tgItem.setproctect3(false);
          pc.setproctctran(0);
          tgItem.setEnchantLevel(0);
          pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
          pc.getInventory().updateItem(tgItem, 4);
          pc.getInventory().saveItem(tgItem, 4);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("受到初级装备保护卷轴的影响,失败后物品归0。"));
          RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), 
              item.getId(), "受到机率初卷的保护", pc.getIp());
        } else {
          pc.setproctctran(0);
          enchantWeapon.failureEnchant(pc, tgItem);
        } 
      } else if (tgItem.getproctect4()) {
        if (this.random.nextInt(100) + 1 <= pc.getproctctran()) {
          tgItem.setproctect4(false);
          pc.setproctctran(0);
          tgItem.setEnchantLevel(tgItem.getEnchantLevel() - 1);
          pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
          pc.getInventory().updateItem(tgItem, 4);
          pc.getInventory().saveItem(tgItem, 4);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("受到中级装备保护卷轴的影响,失败后物品倒扣1。"));
          RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), 
              item.getId(), "受到机率中卷的保护", pc.getIp());
        } else {
          pc.setproctctran(0);
          enchantWeapon.failureEnchant(pc, tgItem);
        } 
      } else if (tgItem.getproctect5()) {
        if (this.random.nextInt(100) + 1 <= pc.getproctctran()) {
          tgItem.setproctect5(false);
          pc.setproctctran(0);
          pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
          pc.getInventory().updateItem(tgItem, 4);
          pc.getInventory().saveItem(tgItem, 4);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("受到高级装备保护卷轴的影响,失败后物品无变化。"));
          RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), 
              item.getId(), "受到机率高卷的保护", pc.getIp());
        } else {
          pc.setproctctran(0);
          enchantWeapon.failureEnchant(pc, tgItem);
        } 
      } else {
        enchantWeapon.failureEnchant(pc, tgItem);
        if (!tgItem.getproctect() || !tgItem.getproctect1() || !tgItem.getproctect2())
          RecordTable.get().recordFailureArmor(pc.getName(), item.getAllName(), tgItem.getAllName(), tgItem.getEnchantLevel(), 
              item.getId(), "未受保护爆了", pc.getIp()); 
      } 
      pc.getInventory().removeItem(item, 1L);
    } 
  }
}
